package bg.btanev.eightyfour.domain.profile.feed.processor;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.Validate;

import bg.btanev.eightyfour.domain.geo.GeoLocation;

import com.sun.syndication.feed.module.georss.GeoRSSModule;
import com.sun.syndication.feed.module.georss.geometries.Position;
import com.sun.syndication.feed.synd.SyndEntry;

/**
 *
 * @author B.Tanev
 *
 */
public class GeoRssProcessor extends BaseRssModulesProcessor<GeoLocation, GeoRSSModule> {
	/**
	 * the namespace URI for georss simple: <em>"http://www.georss.org/georss"</em>
	 */
	private static final String GEORSS_GEORSS_URI = "http://www.georss.org/georss";

	/**
	 * the namespace URI for w3c georss: <em>"http://www.w3.org/2003/01/geo/wgs84_pos#"</em>
	 */
	private static final String GEORSS_W3CGEO_URI = "http://www.w3.org/2003/01/geo/wgs84_pos#";

	/**
	 * the namespace URI for GML georss: <em>"http://www.opengis.net/gml"</em>
	 */
	private static final String GEORSS_GML_URI = "http://www.opengis.net/gml";

	public GeoRssProcessor(final SyndEntry feedEntry) {
		super(feedEntry);
	}

	@Override
	protected String[] getModuleNamespaces() {
		return new String[] { GEORSS_GEORSS_URI, GEORSS_GML_URI, GEORSS_W3CGEO_URI };
	}

	@Override
	protected Collection<GeoLocation> processModuleInternal(final GeoRSSModule rssModule) {
		Validate.notNull(rssModule);

		Set<GeoLocation> locations = new HashSet<GeoLocation>(1);

		Position position = rssModule.getPosition();
		if (position != null) {
			locations.add(new GeoLocation(position.getLatitude(), position.getLongitude()));
		}

		if (locations.isEmpty()) {
			locations = null;
		}

		return locations;
	}
}
